/*
 * Copyright (C) 2011 Project R'yleh Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.Arkham;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import android.media.MediaPlayer;

public class VocabGame extends Activity implements OnClickListener {

	private final String TAG = "Vocab Game";
	
	//"parallel" ArrayLists of terms and corresponding definitions
	private ArrayList<String> terms = new ArrayList<String>();
	private ArrayList<String> defs = new ArrayList<String>();
	
	private int currIndex = -1;  //index of the most recent term used
	private Random rand = new Random(); //used to create random index in Shuffle()
	
	private boolean ans = false;  //did user answer question correctly?
	
	private Intent intent;
	private Bundle bundle;
	
	private String question, a, b, c, d;  //used to hold values for a multiple choice question
	private AssetManager aM;
	private Long startTime, endTime, time;  //time to answer question
	
	private TextView Question;
	private TextView A;
	private TextView B;
	private TextView C;
	private TextView D;
	
	MediaPlayer wrongSound, rightSound;						//Sound

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vocab);
		aM = getResources().getAssets();

		rightSound = MediaPlayer.create(this, R.raw.right_sound);		//Set sound effect for correct entry
		wrongSound = MediaPlayer.create(this, R.raw.wrong_sound);		//Set sound effect for incorrect entry
		
		try{
			Scanner reader = new Scanner(aM.open("vocab.txt"));
			while(reader.hasNextLine()){
				//get first word in line to be term
				terms.add(reader.next());
				//get the rest of the line to be the definition
				defs.add(reader.nextLine());
			}
		}catch(IOException e){
			Log.getStackTraceString(e);
			Log.e(TAG, e.getMessage());
		}
		
		View btnA = findViewById(com.Arkham.R.id.btnA);
		btnA.setOnClickListener(this);
		View btnB = findViewById(com.Arkham.R.id.btnB);
		btnB.setOnClickListener(this);
		View btnC = findViewById(com.Arkham.R.id.btnC);
		btnC.setOnClickListener(this);
		View btnD = findViewById(com.Arkham.R.id.btnD);
		btnD.setOnClickListener(this);

		Question = (TextView)findViewById(R.id.VocabQuestion);
		A = (TextView)findViewById(R.id.choiceA);
		B = (TextView)findViewById(R.id.choiceB);
		C = (TextView)findViewById(R.id.choiceC);
		D = (TextView)findViewById(R.id.choiceD);
		
		this.shuffle();
		this.nextTermQuestion();
		Question.setText(this.getQuestion());
		A.setText("A) " + this.getA());
		B.setText("B) " + this.getB());
		C.setText("C) " + this.getC());
		D.setText("D) " + this.getD());
		
		startTime = System.currentTimeMillis();
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) { 
		super.onConfigurationChanged(newConfig); 
		setContentView(R.layout.vocab); 
		
		View btnA = findViewById(com.Arkham.R.id.btnA);
		btnA.setOnClickListener(this);
		View btnB = findViewById(com.Arkham.R.id.btnB);
		btnB.setOnClickListener(this);
		View btnC = findViewById(com.Arkham.R.id.btnC);
		btnC.setOnClickListener(this);
		View btnD = findViewById(com.Arkham.R.id.btnD);
		btnD.setOnClickListener(this);
		
		Question = (TextView)findViewById(R.id.VocabQuestion);
		A = (TextView)findViewById(R.id.choiceA);
		B = (TextView)findViewById(R.id.choiceB);
		C = (TextView)findViewById(R.id.choiceC);
		D = (TextView)findViewById(R.id.choiceD);
		
		Question.setText(this.getQuestion());
		A.setText("A) " + this.getA());
		B.setText("B) " + this.getB());
		C.setText("C) " + this.getC());
		D.setText("D) " + this.getD());
	}

	public void onClick(View v) {
		endTime = System.currentTimeMillis();
		time = endTime - startTime;
    	switch (v.getId())
    	{
    	case com.Arkham.R.id.btnA:
    		//if A is the answer
    		if(this.myDef(this.getQuestion()).equals(this.getA())){
    			ans = true;
    			if(rightSound != null)
    				rightSound.start();
    		}
    		else{
    			ans = false;
    			if(wrongSound != null)
    				wrongSound.start();
    		}
    		//send result to main activity
    		intent = new Intent();
    		bundle = new Bundle();
    		bundle.putBoolean("ANS", ans);
    		bundle.putLong("TIME", time);
    		intent.putExtras(bundle);
    		setResult(Activity.RESULT_OK, intent);
			finish();   		
    		break;
    	case com.Arkham.R.id.btnB:
    		//if B is the answer
    		if(this.myDef(this.getQuestion()).equals(this.getB())){
    			ans = true;
    			if(rightSound != null)
    				rightSound.start();
    		}
    		else{
    			ans = false;
    			if(wrongSound != null)
    				wrongSound.start();
    		}
    		//send result to main activity
    		intent = new Intent();
    		bundle = new Bundle();
    		bundle.putBoolean("ANS", ans);
    		bundle.putLong("TIME", time);
    		intent.putExtras(bundle);
    		setResult(Activity.RESULT_OK, intent);
			finish();
    		break;
    	case com.Arkham.R.id.btnC:
    		//if C is the answer
    		if(this.myDef(this.getQuestion()).equals(this.getC())){
    			ans = true;
    			if(rightSound != null)
    				rightSound.start();
    		}
    		else{
    			ans = false;
    			if(wrongSound != null)
    				wrongSound.start();
    		}
    		//send result to main activity
    		intent = new Intent();
    		bundle = new Bundle();
    		bundle.putBoolean("ANS", ans);
    		bundle.putLong("TIME", time);
    		intent.putExtras(bundle);
    		setResult(Activity.RESULT_OK, intent);
			finish();
    		break;
    	case com.Arkham.R.id.btnD:
    		//if D is the answer
    		if(this.myDef(this.getQuestion()).equals(this.getD())){
    			ans = true;
    			if(rightSound != null)
    				rightSound.start();
    		}
    		else{
    			ans = false;
    			if(wrongSound != null)
    				wrongSound.start();
    		}
    		//send result to main activity
    		intent = new Intent();
    		bundle = new Bundle();
    		bundle.putBoolean("ANS", ans);
    		bundle.putLong("TIME", time);
    		intent.putExtras(bundle);
    		setResult(Activity.RESULT_OK, intent);
			finish();
    		break;
    	}
		
	}
	
	public String getQuestion(){
		return question;
	}
	
	public String getA(){
		return a;
	}
	
	public String getB(){
		return b;
	}
	
	public String getC(){
		return c;
	}
	
	public String getD(){
		return d;
	}
	
	/*
	 * print terms to console
	 */
	public void printTerms(){
		for(int i=0; i<terms.size(); i++){
			System.out.println(terms.get(i));
		}
	}
	
	/*
	 * print definitions to console
	 */
	public void printDefs(){
		for(int i=0; i<defs.size(); i++){
			System.out.println(defs.get(i));
		}
	}
	
	/*
	 * return next term
	 */
	public String nextTerm(){
		if(currIndex>=terms.size()-1){
			currIndex = -1;  //start list over
		}
		currIndex++;
		return terms.get(currIndex);
	}
	
	/*
	 * return next definition
	 */
	public String nextDef(){
		if(currIndex>=defs.size()-1){
			currIndex = -1;  //start list over
		}
		currIndex++;
		return defs.get(currIndex);
	}
	
	/*
	 * return corresponding term
	 */
	public String myTerm(String def){
		return terms.get(defs.indexOf(def));
	}
	
	/*
	 * return corresponding def
	 */
	public String myDef(String term){
		return defs.get(terms.indexOf(term));
	}
	
	/*
	 * shuffle the order of terms/definitions
	 */
	public void shuffle(){
		String tempTerm = "";
		String tempDef = "";
		int randIndex = 0;
		for(int i=0; i<terms.size(); i++){
			randIndex = rand.nextInt(terms.size());
			//swap terms[i] and terms[j]
			tempTerm = terms.get(i);
			terms.set(i, terms.get(randIndex));
			terms.set(randIndex, tempTerm);
			
			//swap defs[i] and defs[j]
			tempDef = defs.get(i);
			defs.set(i, defs.get(randIndex));
			defs.set(randIndex, tempDef);
		}
		currIndex = -1;
	}
	
	/*
	 * Produce 1 term question and 4 possible definition answers
	 */
	public void nextTermQuestion(){
		question = nextTerm();
		int correct = rand.nextInt(4);
		if(correct == 0){
			a = myDef(question);
			b = nextDef();
			c = nextDef();
			d = nextDef();
		}
		else if(correct == 1){
			a = nextDef();
			b = myDef(question);
			c = nextDef();
			d = nextDef();
		}		
		else if(correct == 2){
			a = nextDef();
			b = nextDef();
			c = myDef(question);
			d = nextDef();
		}		
		else{
			a = nextDef();
			b = nextDef();
			c = nextDef();
			d = myDef(question);
		}
		
	}
	
	/*
	 * Produce 1 definition question and 4 possible term answers
	 */
	public void nextDefQuestion(){
		question = nextDef();
		int correct = rand.nextInt(4);
		if(correct == 0){
			a = myTerm(question);
			b = nextTerm();
			c = nextTerm();
			d = nextTerm();
		}
		else if(correct == 1){
			a = nextTerm();
			b = myTerm(question);
			c = nextTerm();
			d = nextTerm();
		}		
		else if(correct == 2){
			a = nextTerm();
			b = nextTerm();
			c = myTerm(question);
			d = nextTerm();
		}		
		else{
			a = nextTerm();
			b = nextTerm();
			c = nextTerm();
			d = myTerm(question);
		}
		
	}
	
}

